package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.service.impl.OrdersServiceImpl;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public int save(List<Orderdetailet> orderdetailet) throws Exception{
        int result = 0;
        String sql ="insert into orderdetailet(orderId,foodId,quantity) " +
                " value(?,?,?)";
        for (int i = 0; i < orderdetailet.size(); i++) {
            sql.concat(" ,value(?,?,?)");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count =1;
            for (Orderdetailet od : orderdetailet) {
                pst.setInt(count++,od.getOrderId());
                pst.setInt(count++,od.getFoodId());
                pst.setInt(count++,od.getQuantity());
            }
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return result;
    }
}
